﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Windows.Forms;

namespace RibbonEditor
{
    class XmlHelper
    {
        public enum PropertyType
        { 
            stringType,
            integerType,
            booleanType,
            decimalType,
            floatType
        }

        public static XmlNode AddNode(XmlNode parentNode, string nodeName)
        {
            XmlNode node = null;

            if (parentNode.OwnerDocument == null && parentNode is XmlDocument)
            {
                node = ((XmlDocument)parentNode).CreateElement(nodeName);
                ((XmlDocument)parentNode).DocumentElement.AppendChild(node);
            }
            else
            {
                node = parentNode.OwnerDocument.CreateElement(nodeName);
                parentNode.AppendChild(node);
            }

            return node;
        }

        public static void AddAttribute(XmlNode parentNode, string attributeName, object value)
        {
            try
            {
                if (value != null)
                {
                    if ((value.GetType() != typeof(string)) || (value.GetType() == typeof(string) && value.ToString().Length > 0))
                    {
                        XmlAttribute attribute = parentNode.OwnerDocument.CreateAttribute(attributeName);

                        attribute.Value = value.ToString();

                        if (value is bool)
                        {
                            attribute.Value = attribute.Value.ToLower();
                        }

                        if (value.GetType().BaseType == typeof(Enum))
                        {
                            if (value.GetType() == typeof(Enumerations.WinMode) || value.GetType() == typeof(Enumerations.WebResourceType))
                            {
                                attribute.Value = ((int)value).ToString();
                            }
                        }

                        parentNode.Attributes.Append(attribute);
                    }
                }
            }
            catch (Exception error)
            {
                MessageBox.Show("An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        public static string GetAttribute(XmlNode node, string attributeName)
        {
            if (node != null && node.Attributes[attributeName] != null)
            {
                return node.Attributes[attributeName].Value;
            }
            else
            {
                return string.Empty;
            }
        }

        public static int? GetNullableIntAttribute(XmlNode node, string attributeName)
        {
            if (node != null && node.Attributes[attributeName] != null)
            {
                string value = node.Attributes[attributeName].Value;

                int i;
                if (Int32.TryParse(value, out i))
                {
                    return i;
                }
                else
                { 
                    return null;
                }
            }
            else
            {
                return null;
            }
        }

        public static decimal? GetNullableDecimalAttribute(XmlNode node, string attributeName)
        {
            if (node != null && node.Attributes[attributeName] != null)
            {
                string value = node.Attributes[attributeName].Value;

                decimal i;
                if (decimal.TryParse(value, out i))
                {
                    return i;
                }
                else
                {
                    return null;
                }
            }
            else
            {
                return null;
            }
        }

        public static void ExtractXmlAttributeToProperty(XmlNode node, string attributeName, object property, PropertyType pType)
        {
            if (node.Attributes[attributeName] != null && node.Attributes[attributeName].Value.Length > 0)
            {
                if (pType == PropertyType.stringType)
                {
                    property = node.Attributes[attributeName].Value;
                }
                else if (pType == PropertyType.integerType)
                {
                    property = int.Parse(node.Attributes[attributeName].Value);
                }
                else if (pType == PropertyType.decimalType)
                {
                    property = decimal.Parse(node.Attributes[attributeName].Value);
                }
                else if (pType == PropertyType.floatType)
                {
                    property = float.Parse(node.Attributes[attributeName].Value);
                }
                else if (pType == PropertyType.booleanType)
                {
                    property = node.Attributes[attributeName].Value == "1";
                }
            }
        }

        public static object ExtractXmlAttributeToProperty2(XmlNode node, string attributeName, PropertyType pType)
        {
            if (node.Attributes[attributeName] != null && node.Attributes[attributeName].Value.Length > 0)
            {
                if (pType == PropertyType.stringType)
                {
                    return node.Attributes[attributeName].Value;
                }
                else if (pType == PropertyType.integerType)
                {
                    return int.Parse(node.Attributes[attributeName].Value);
                }
                else if (pType == PropertyType.decimalType)
                {
                    return decimal.Parse(node.Attributes[attributeName].Value);
                }
                else if (pType == PropertyType.floatType)
                {
                    return float.Parse(node.Attributes[attributeName].Value);
                }
                else if (pType == PropertyType.booleanType)
                {
                    return node.Attributes[attributeName].Value == "1";
                }
            }

            return null;
        }

        public static void ExtractXmlAttributeToEnumProperty(XmlNode node, string attributeName, object property)
        {
            try
            {
                if (node.Attributes[attributeName] != null)
                {
                    property = Enum.Parse(property.GetType(), node.Attributes[attributeName].Value);
                }
            }
            catch (Exception error)
            {
                MessageBox.Show("An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
    }
}
